package org.misalen.web.restful.util;

import org.json.JSONObject;
import org.misalen.web.restful.job.RestfulService;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.misalen.retrofit.HttpResponse;
import com.misalen.retrofit.RetrofitConverterFactory;
import com.misalen.retrofit.adapter.rxjava.RxJavaCallAdapterFactory;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * 
 * http操作类
 * 
 * @author misaya
 * 
 */
public class RetrofitUtil {

	private static String baseUrl;
	private Retrofit retrofit;
	private static RestfulService restfulService;
	private static RetrofitUtil retrofitUtil = null;

	public static RetrofitUtil setBaseUrl(String baseUrl) {
		RetrofitUtil.baseUrl = baseUrl;
		if (retrofitUtil == null) {
			retrofitUtil = new RetrofitUtil();
		}
		return retrofitUtil;
	}

	private RetrofitUtil() {
		Gson gson = new GsonBuilder().serializeNulls().create();
		RetrofitConverterFactory factory = RetrofitConverterFactory.create(gson);
		retrofit = new Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(factory)
				.addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();
		restfulService = retrofit.create(RestfulService.class);
	}

	public static RestfulService getService() {
		if (retrofitUtil == null) {
			retrofitUtil = new RetrofitUtil();
		}
		return restfulService;
	}

	public static RetrofitUtil getInstance() {
		if (retrofitUtil == null) {
			retrofitUtil = new RetrofitUtil();
		}
		return retrofitUtil;
	}

	// public Observable<T> enqueue(Call<FitResult<T>> call, final
	// HttpResponse<T> httpResponse) {
	// call.enqueue(new Callback<FitResult<T>>() {
	// @Override
	// public void onFailure(Call<FitResult<T>> arg0, Throwable arg1) {
	// httpResponse.onRequestFailure(arg1);
	// }
	//
	// @Override
	// public void onResponse(Call<FitResult<T>> arg0, Response<FitResult<T>>
	// arg1) {
	// int status = arg1.code();
	// if (status == 200) {
	// status = arg1.body().getErrorCode();
	// if (status == 200) {
	// httpResponse.onSuccess(arg1.body().getData());
	// } else {
	// httpResponse.onFailure(status, arg1.body().getMessage());
	// }
	// } else {
	// httpResponse.onFailure(status, "请求失败");
	// }
	// }
	// });
	// }

	public void enqueueJson(Call<JSONObject> call, final HttpResponse<JSONObject> httpResponse) {

		call.enqueue(new Callback<JSONObject>() {

			@Override
			public void onFailure(Call<JSONObject> arg0, Throwable arg1) {
				httpResponse.onRequestFailure(arg1);
			}

			@Override
			public void onResponse(Call<JSONObject> arg0, Response<JSONObject> arg1) {
				int status = arg1.code();
				if (status == 200) {
					status = arg1.body().getInt("errorCode");
					if (status == 200) {
						if (arg1.body().get("data") instanceof JSONObject) {
							httpResponse.onSuccess(arg1.body().getJSONObject("data"));
						} else {
							httpResponse.onSuccess(null);
						}
					} else {
						httpResponse.onFailure(status, arg1.body().getString("message"));
					}
				} else {
					httpResponse.onFailure(status, "请求失败");
				}
			}
		});
	}
}
